package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.trading.operate.parameter.req.OrderLogisticsInfoPo;
import com.ymatou.iapi.trading.operate.parameter.req.SyncDomesticDeliveredInfoBean;
import com.ymatou.iapi.trading.operate.service.LogisticsDeliveryService;
import com.ymatou.iapi.trading.operate.service.SyncDomesticDeliveredInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.TestDataManager;

/**
 * 同步国内发货信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SyncDomesticDeliveredInfo {
	private static SyncDomesticDeliveredInfoBean syncdomesticdeliveredinfoBean;
	private static SyncDomesticDeliveredInfoCall syncdomesticdeliveredinfoCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("同步国内发货信息接口");
	}

	@Before
	public void caseUp() {
		syncdomesticdeliveredinfoBean = new SyncDomesticDeliveredInfoBean();
		syncdomesticdeliveredinfoCall = new SyncDomesticDeliveredInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syncdomesticdeliveredinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDeliveredInfo_001() {
		Logger.start(true, "同步国内发货信息成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			Map map = new OrderCallService()
					.createM2cOrder(LogisticsTypeEnum.PackageDelivery);
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);

			Logger.comment("前置数据准备结束");

			syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
			syncdomesticdeliveredinfoBean.setOrderId(orderId);
			syncdomesticdeliveredinfoBean.setLogisticsNo("autotest_"
					+ TestDataManager.getRandomNum(5));
			syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
			Logger.comment("同步国内发货信息前");
			// 验证订单表
			List<Map> list = new DefaultWapper().selectOrderByOderId(orderId);
			Logger.verifyEquals(null, list.get(0).get("bDomesticDelivered"),
					"验证订单表bDomesticDelivered");
			syncdomesticdeliveredinfoCall
					.setData(syncdomesticdeliveredinfoBean);
			syncdomesticdeliveredinfoCall.callService();
			Logger.comment("同步国内发货信息后");
			Logger.verifyEquals("ok",
					syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
			List<Map> list2 = new DefaultWapper().selectOrderByOderId(orderId);
			Logger.debug("bDomesticDelivered:"
					+ list2.get(0).get("bDomesticDelivered"));
			Logger.verifyEquals(true,
					(Boolean) list2.get(0).get("bDomesticDelivered"),
					"验证订单表bDomesticDelivered");

			OrderLogisticsInfoPo orderLogisticsInfoPo = LogisticsDeliveryService
					.getLogisticsDeliveryByOrderId(orderId).get(1);

			Logger.verifyEquals(syncdomesticdeliveredinfoBean.getLogisticsNo(),
					orderLogisticsInfoPo.getBillCode(),
					"验证Ymt_OrderLogisticsInfo 表 BillCode");
			Logger.verifyEquals(
					syncdomesticdeliveredinfoBean.getLogisticsCompany(),
					orderLogisticsInfoPo.getCompany(),
					"验证Ymt_OrderLogisticsInfo 表 Company");

			Logger.verifyEquals(2, orderLogisticsInfoPo.getDeliverType(),
					"验证Ymt_OrderLogisticsInfo 表 deliverType");

			DataUtil.verifyDecimalEquals(
					syncdomesticdeliveredinfoBean.getWeight(),
					orderLogisticsInfoPo.getWeight(),
					"验证Ymt_OrderLogisticsInfo 表 Weight");

			Logger.verifyEquals(true, orderLogisticsInfoPo.getChinaDeliver(),
					"验证Ymt_OrderLogisticsInfo 表 chinaDeliver");

			Logger.verifyEquals(false,
					orderLogisticsInfoPo.getOfficialLogistics(),
					"验证Ymt_OrderLogisticsInfo 表 officialLogistics");

			Logger.verifyEquals(false, orderLogisticsInfoPo.getDeleted(),
					"验证Ymt_OrderLogisticsInfo 表 deleted");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDeliveredInfo_002() {
		Logger.start(true, "订单号不存在");
		try {
			syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
			syncdomesticdeliveredinfoBean.setOrderId(1111);
			syncdomesticdeliveredinfoBean.setLogisticsNo("22222222222");
			syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
			syncdomesticdeliveredinfoCall
					.setData(syncdomesticdeliveredinfoBean);
			syncdomesticdeliveredinfoCall.callService();
			Logger.verifyEquals("ok",
					syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDeliveredInfo_003() {
		Logger.start(true, "订单号为空");
		try {
			syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
			// syncdomesticdeliveredinfoBean.setOrderId(1111);
			syncdomesticdeliveredinfoBean.setLogisticsNo("22222222222");
			syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
			syncdomesticdeliveredinfoCall
					.setData(syncdomesticdeliveredinfoBean);
			syncdomesticdeliveredinfoCall.callService();
			Logger.verifyEquals("fail",
					syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SyncDomesticDeliveredInfo_004() {
		Logger.start(true, "多物流单号,同步国内发货信息成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			Map map = new OrderCallService()
					.createM2cOrder(LogisticsTypeEnum.PackageDelivery);
			int orderId = (Integer) map.get("orderId");
			int sellerId = (Integer) map.get("sellerId");
			int userId = (Integer) map.get("userId");
			int tradingId = (Integer) map.get("tradingId");
			BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);

			Logger.comment("前置数据准备结束");

			syncdomesticdeliveredinfoBean.setLogisticsCompany("码头自动化测试物流");
			syncdomesticdeliveredinfoBean.setOrderId(orderId);
			syncdomesticdeliveredinfoBean.setLogisticsNo("autotest_"
					+ TestDataManager.getRandomNum(5)
					+ ",sdfsdf,qwerwe,autotestno");
			syncdomesticdeliveredinfoBean.setWeight(new BigDecimal(2));
			Logger.comment("同步国内发货信息前");

			// 验证订单表
			List<Map> list = new DefaultWapper().selectOrderByOderId(orderId);
			Logger.verifyEquals(null, list.get(0).get("bDomesticDelivered"),
					"验证订单表bDomesticDelivered");
			syncdomesticdeliveredinfoCall
					.setData(syncdomesticdeliveredinfoBean);
			syncdomesticdeliveredinfoCall.callService();
			Logger.comment("同步国内发货信息后");
			Logger.verifyEquals("ok",
					syncdomesticdeliveredinfoCall.getReturnData(), "验证接口返回状态");
			List<Map> list2 = new DefaultWapper().selectOrderByOderId(orderId);
			Logger.debug("bDomesticDelivered:"
					+ list2.get(0).get("bDomesticDelivered"));
			Logger.verifyEquals(true,
					(Boolean) list2.get(0).get("bDomesticDelivered"),
					"验证订单表bDomesticDelivered");

			List<OrderLogisticsInfoPo> orderLogisticsInfoPoList = LogisticsDeliveryService
					.getLogisticsDeliveryByOrderId(orderId);

			List<OrderLogisticsInfoPo> orderLogisticsInfoPoList2 = orderLogisticsInfoPoList
					.stream()
					.filter(logistics -> logistics.getDeliverType() == 2)
					.collect(Collectors.toList());

			for (int i = 0; i < orderLogisticsInfoPoList2.size(); i++) {
				OrderLogisticsInfoPo orderLogisticsInfoPo = orderLogisticsInfoPoList2
						.get(i);

				Logger.verifyEquals(syncdomesticdeliveredinfoBean
						.getLogisticsNo().split(",")[i], orderLogisticsInfoPo
						.getBillCode(), "验证Ymt_OrderLogisticsInfo 表 BillCode");
				Logger.verifyEquals(
						syncdomesticdeliveredinfoBean.getLogisticsCompany(),
						orderLogisticsInfoPo.getCompany(),
						"验证Ymt_OrderLogisticsInfo 表 Company");

				Logger.verifyEquals(2, orderLogisticsInfoPo.getDeliverType(),
						"验证Ymt_OrderLogisticsInfo 表 deliverType");

				DataUtil.verifyDecimalEquals(
						syncdomesticdeliveredinfoBean.getWeight(),
						orderLogisticsInfoPo.getWeight(),
						"验证Ymt_OrderLogisticsInfo 表 Weight");

				Logger.verifyEquals(true,
						orderLogisticsInfoPo.getChinaDeliver(),
						"验证Ymt_OrderLogisticsInfo 表 chinaDeliver");

				Logger.verifyEquals(false,
						orderLogisticsInfoPo.getOfficialLogistics(),
						"验证Ymt_OrderLogisticsInfo 表 officialLogistics");

				Logger.verifyEquals(false, orderLogisticsInfoPo.getDeleted(),
						"验证Ymt_OrderLogisticsInfo 表 deleted");

			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
